# coding:utf-8
import paramiko


class gateOperate:
    def __init__(self, host, port, username, password):
        self.host = host
        self.port = port
        self.username = username
        self.password = password
        # 建立SSH连接
        self.ssh = paramiko.SSHClient()
        self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        self.ssh.connect(self.host, self.port, self.username, self.password)
        print('连接网关成功')

    def sendCommand(self, command):
        # 执行远程命令
        stdin, stdout, stderr = self.ssh.exec_command(f"bash {command}")
        print('已执行命令，等待结果中')
        stdin.close()
        # 获取命令输出和错误信息
        output = stdout.read().decode('utf-8')
        error = stderr.read().decode('utf-8')
        # 如果有错误，打印错误信息
        if error:
            print("Error:", error)

        return output,error






if __name__ == '__main__':
    # SSH连接参数
    hostname = '192.168.3.92'
    port = 22
    username = 'pi'
    password = 'gwza2017999'
    # # 远程脚本路径
    command = 'wlog'
    gateOb = gateOperate(hostname, port, username, password)
    output = gateOb.sendCommand(command)
    print('' in output)

